<template>
  <div class="layout-flex">
    <header>头部</header>
    <main id="content">
      <ul>
        <li v-for="(item, index) in list" :key="index">
          {{ item }}
        </li>
      </ul>
    </main>
    <footer>
      <input type="text" placeholder="Footer..." v-model="content" />
      <button class="submit" @click="handleSubmit">提交</button>
    </footer>
  </div>
</template>

<script>
import axios from 'axios';
const CryptoJS = require('crypto-js');

export default {
  name: 'index',
  data() {
    return {
      content: '',
      list: []
    };
  },
  watch: {
    list() {
      this.scrollIntoView();
    }
  },
  methods: {
    // todo TIPS：页面（容器）可滚动时才有用！
    // Element.scrollIntoView() 方法让当前的元素滚动到浏览器窗口的可视区域内。
    // element.scrollIntoView() 参数默认为true。
    // 参数为true时调用该函数，页面（或容器）发生滚动，使element的顶部与视图（容器）顶部对齐；
    // 参数为false时，使element的底部与视图（容器）底部对齐。
    // document.getElementById("pToShow").scrollIntoView(); 滚动到顶部
    // document.getElementById("pToShow").scrollIntoView(false); 滚动到底部
    scrollIntoView() {
      console.log('scrollIntoView-===');
      this.$nextTick(() => {
        const dom = document.querySelector('#content');
        console.log(dom.scrollHeight);
        // dom.scrollIntoView(false);
        dom.scrollTo(0, dom.scrollHeight);
      });
    },
    handleSubmit() {
      this.list.push(this.content);
      this.content = '';
    }
  },
  mounted() {
    console.log('mounted====');
    const parmas = {
      appKey: '4ff7c9406e2e4b73960bbce8fdedd7d6',
      appId: '18e44bdd-7d49-447b-9a3d-b88bafc7cc79',
      apiId: 'cf4c8520-a574-11eb-9842-3d552d7de422'
    };
    const timestamp = new Date().getTime() / 1000;
    const signature = parmas.appId + parmas.apiId + timestamp;

    const key = CryptoJS.enc.Utf8.parse(parmas.appKey);
    console.log('key==', key);
    let encrypted = CryptoJS.AES.encrypt(signature, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    }).toString();
    console.log('encrypted==', encrypted);

    // let encryptedHexStr = CryptoJS.enc.Hex.parse(encrypted);
    // const aaa = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    // console.log('aaa==', aaa);

    axios
      .post(
        'https://budp-test.manniuhealth.com/clientgateway/',
        {
          registerId: '91339992923091036'
        },
        {
          headers: {
            'content-type': 'application/json',
            apiname: parmas.apiId,
            appid: parmas.appId,
            signature: encrypted
          }
        }
      )
      .then(function(response) {
        console.log(response);
      })
      .catch(function(error) {
        console.log(error);
      });
  }
};
</script>

<style lang="scss" scoped>
.layout-flex {
  height: 100vh;
  display: flex;
  flex-direction: column;
  header {
    position: fixed;
    width: 100%;
    height: 50px;
    left: 0;
    top: 0;
    background-color: #42b983;
    color: #fff;
    font-size: 18px;
    text-align: center;
    line-height: 50px;
  }
  footer {
    width: 100%;
    height: 34px;
    display: flex;
    background-color: #586c94;

    input {
      flex: 1;
      background-color: #fff;
    }
    .submit {
      background-color: #2c8dfb;
      width: 70px;
      height: 100%;
      color: #fff;
      outline: none;
      border: none;
    }
  }
  main {
    flex: 1;
    padding-top: 50px;
    overflow-y: scroll;
    overflow-x: hidden;

    li {
      padding: 10px 0;
      font-size: 16px;
    }
  }
}
</style>
